<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 设置页面高度为100% */
        html, body {
            height: 100%;
        }
        /* 使用Flexbox使内容居中 */
        .full-height {
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
</head>
<body>
    <div id="app" class="full-height">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="text-center">用户登录</h3>
                        </div>
                        <div class="card-body">
                            <form @submit.prevent="login">
                                <div class="mb-3">
                                    <label for="username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="username" v-model="username" required>
                                </div>
                                <div class="mb-3">
                                    <label for="password" class="form-label">密码</label>
                                    <input type="password" class="form-control" id="password" v-model="password" required>
                                </div>
                                <button type="submit" class="btn btn-primary w-100">登录</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
       
    </div>

    <!-- Vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="./users.js"></script>

    <script>
        new Vue({
            el: '#app',
            data: {
                username: '', // 绑定的用户名
                    password: '', // 绑定的密码
                    message: '',  // 显示的消息
                    isSuccess: false // 是否登录成功
            },
            methods: {
                async login() {
                    const storedPassword = getPasswordByUsername(this.username);
                    if (storedPassword && storedPassword === this.password) {
                        this.message = "Login successful!";
                        this.isSuccess = true;

                        // 登录成功后，将用户名发送到后端
                        const response = await fetch('http://192.168.43.147:4000/get-photo', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({ username: this.username })
                        });

                        if (response.ok) {
                            const data = await response.json();
                            // 跳转到 photo.html 并传递用户名
                            window.location.href = `photo.html?username=${this.username}&photoUrl=${data.photoUrl}`;
                        } else {
                            this.message = "Failed to fetch photo.";
                            this.isSuccess = false;
                        }
                    } else {
                        this.message = "Invalid username or password.";
                        this.isSuccess = false;
                    }
                }
            }
            
        });
    </script>
</body>
</html>